package com.companyname.projecttemplate.ws;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RestController;

import com.base.core.context.annotation.RestfulCheck;
import com.base.core.mvc.web.BaseWebController;
import com.base.core.mvc.ws.WsState;
import com.companyname.projecttemplate.ao.ws.WsSessionAO;
import com.companyname.projecttemplate.component.SendService;
import com.companyname.projecttemplate.mapping.WsMapping;
import com.gitee.magic.framework.head.vo.BaseVO;
import com.gitee.magic.framework.head.vo.ObjectVO;

import jakarta.annotation.Resource;

/**
 * @author start
 */
@RestController
public class WsController extends BaseWebController implements WsMapping {

	    @Resource
	    private StringRedisTemplate stringRedisTemplate;

	    @RestfulCheck
	    @Override
		public ObjectVO<Boolean> online(String userId) {
	    	return response(stringRedisTemplate.hasKey(WsState.getWebSocketUserId(userId)));
		}

		@RestfulCheck
	    @Override
		public ObjectVO<String> channel(String userId) {
			String channelKey=WsState.getWebSocketUserId(userId);
			String channel=null;
			if(stringRedisTemplate.hasKey(channelKey)) {
				channel=stringRedisTemplate.opsForValue().get(channelKey);
			}
			return response(channel);
		}
	    
	    @RestfulCheck
	    @Override
		public BaseVO send(WsSessionAO ao) {
	    	SendService.ws(ao.getUserId(), ao.getContent(), false);
	    	return response();
		}
	
}
